#ifndef __RSCRIPTGENERATOR__
#define __RSCRIPTGENERATOR__

#include <ostream>
#include <iostream>
#include <fstream>
#include <string>
#include "CommandParser.h"

using namespace std;

class RScriptGenerator{
	CommandParser* arg;
public:
	RScriptGenerator(CommandParser* cmd){
		arg = cmd;
	}
	void generateScript(){
		// remove extension at the end of .map filename
		string file = arg->mapvalue;
		
		for (auto p = file.begin(); p !=  file.end(); p++){
			if (*p == '.'){
				*p = '\0';
				break;
			}
		}
		
		printf("Writing R script\n");

		file = file + "Rscript";

		ofstream outfile;
		outfile.open(file, ios::out);

		outfile << "library(SKAT)" << endl;
		outfile << "bed<-\"" << file << ".bed\"" << endl;
		outfile << "bim<-\"" << file << ".bim\"" << endl;
		outfile << "fam<-\"" << file << ".fam\"" << endl;
		outfile << "setid<-\"" << file << ".SetID\"" << endl;
		outfile << "ssd<-\"" << file << ".SSD\"" << endl;
		outfile << "info<-\"" << file << ".SSD.info\"" << endl;
		outfile << "FAM<-Read_Plink_FAM(fam,Is.binary=TRUE)" << endl;
		outfile << "y<-FAM$Phenotype" << endl;
		outfile << "y=y-1" << endl;
		outfile << "SSD.INFO<-Open_SSD(ssd,info)" << endl;
		outfile << "obj<-SKAT_Null_Model(y~1,out_type=\"D\")" << endl;
		outfile << "out<-SKAT.SSD.All(SSD.INFO,obj)" << endl;
		outfile << "write.table(out$results.stdout())" << endl;

		outfile.close();
	}
};

#endif